package com.example.educationalsystem.common.util;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletResponse;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @ClassName : ResposeJson  //类名
 * @Description : 返回值封装类  //描述
 * @Author : zpc20 //作者
 * @Date: 2022/12/2  9:08
 */
@Data
@Slf4j
@ApiModel(value = "返回实体对象")
public class ResponseJson {

    /**
     * 返回具体信息
     */
    @ApiModelProperty(value = "返回信息")
    private String msg;
    /**
     * 返回状态信息
     */
    @ApiModelProperty(value = "返回状态信息")
    private Integer status;
    /**
     * 返回具体数据信息
     */
    @ApiModelProperty(value = "返回数据实体列表")
    private Object data;

    /**
    * @Param : [response, json]
    * @Description : 安全框架处理类返回值封装为AjaxJson
    * @Author : zpc20
    * @Date : 2022/12/2 14:56
    * @return void
    * @throws
    */
    public static void responsHandlerJson(ServletResponse response, ResponseJson json){
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.println(JSON.toJSONString(json));
        } catch (Exception e) {
            log.error("【JSON输出异常】"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }


    public static void responseSuccessMap(ServletResponse response, Map<String, Object> resultMap){
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.println(JSON.toJSONString(resultMap));
        } catch (Exception e) {
            log.error("【JSON输出异常】"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }




}
